package com.buanalintas.sandals.pos.returdetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.sandals.pos.entities.PosMasterSize;
import com.buanalintas.sandals.pos.entities.PosTransactionPurchaseOrderDetail;
import com.buanalintas.sandals.pos.entities.PosTransactionReceiptDetail;
import com.buanalintas.sandals.pos.entities.PosTransactionReturDetail;
import java.util.List;
import javax.persistence.EntityManager;

public class ReturDetail extends Page {

    private Field returDetailId;
    private Field returDetailReturId;
    private Field returDetailSizeId;
//    private Field returDetailSize;
    private Field returDetailItemId;
    private Field returDetailQuantity;
    private Field returDetailPrice;
    private Field returDetailDescription;

    public ReturDetail() {
        super();
        setTheClass(PosTransactionReturDetail.class);

        returDetailId = new Field();
        returDetailId.setName("returDetailId");
        returDetailId.setCaption("id");
        returDetailId.setListProperties("");
        returDetailId.setInputProperties("");
        getFields().add(returDetailId);

        returDetailReturId = new Field();
        returDetailReturId.setName("returDetailReturId");
        returDetailReturId.setCaption("id");
        returDetailReturId.setListProperties("");
        returDetailReturId.setInputProperties("");
        getFields().add(returDetailReturId);

        returDetailItemId = new Field();
        returDetailItemId.setName("returDetailItemId");
        returDetailItemId.setCaption("item");
        returDetailItemId.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        returDetailItemId.setInputProperties(" size=\"25\"");
        returDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"5%\" nowrap"});
        returDetailItemId.getSelectorColumns().add(new String[]{"itemName", " Item", "width=\"90%\""});
        returDetailItemId.setSelectorOrder("t.itemName");
        returDetailItemId.setRequired(true);
        getFields().add(returDetailItemId);
        
//        returDetailSize = new Field();
//        returDetailSize.setName("returDetailSize");
//        returDetailSize.setCaption("size");
//        returDetailSize.setListProperties(" align=\"left\" width=\"30%\" nowrap");
//        returDetailSize.setEntityRelation(PosMasterSize.class);
//        returDetailSize.setInputType(Field.INPUT_CHECK_BOX);
//        returDetailSize.setRequired(true);
//        getFields().add(returDetailSize);

        returDetailSizeId = new Field();
        returDetailSizeId.setName("returDetailSizeId");
        returDetailSizeId.setCaption("size");
        returDetailSizeId.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        returDetailSizeId.setEntityRelation(PosMasterSize.class);
        returDetailSizeId.setRequired(true);
        getFields().add(returDetailSizeId);

        returDetailQuantity = new Field();
        returDetailQuantity.setName("returDetailQuantity");
        returDetailQuantity.setCaption("quantity");
        returDetailQuantity.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        returDetailQuantity.setInputProperties(" size=\"5\"");
        returDetailQuantity.setRequired(true);
        getFields().add(returDetailQuantity);

        returDetailPrice = new Field();
        returDetailPrice.setName("returDetailPrice");
        returDetailPrice.setCaption("price");
        returDetailPrice.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        returDetailPrice.setInputProperties(" size=\"8\"");
        returDetailPrice.setRequired(true);
        returDetailPrice.setFormater(MoneyFormatter.getInstance());
        getFields().add(returDetailPrice);

        returDetailDescription = new Field();
        returDetailDescription.setName("returDetailDescription");
        returDetailDescription.setCaption("description");
        returDetailDescription.setListProperties(" width=\"70%\"");
        returDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        returDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        getFields().add(returDetailDescription);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(returDetailItemId);
//        getFieldsAdd().add(returDetailSize);
        getFieldsAdd().add(returDetailQuantity);
//        getFieldsAdd().add(returDetailPrice);
        getFieldsAdd().add(returDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(returDetailItemId);
        getFieldsEdit().add(returDetailSizeId);
        getFieldsEdit().add(returDetailQuantity);
//        getFieldsEdit().add(returDetailPrice);
        getFieldsEdit().add(returDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(returDetailItemId);
        getFieldsList().add(returDetailSizeId);
        getFieldsList().add(returDetailQuantity);
//        getFieldsList().add(returDetailPrice);
        getFieldsList().add(returDetailDescription);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
    }

    @Override
    public void loadDetail(List datas, String id) {
        try {
            EntityManager em = getEntityManager();
            List<PosTransactionReceiptDetail> request = em.createQuery("SELECT t FROM PosTransactionReceiptDetail t WHERE t.receiptDetailReceiptId.receiptId='" + id.toString() + "'").setHint(Persistence.REFRESH_HINT, true).getResultList();
            datas.clear();
            for (PosTransactionReceiptDetail receiptDetail : request) {
                PosTransactionReturDetail rd = new PosTransactionReturDetail();
                rd.setReturDetailItemId(receiptDetail.getReceiptDetailItemId());
                rd.setReturDetailQuantity(receiptDetail.getReceiptDetailQuantity());
                rd.setReturDetailSizeId(receiptDetail.getReceiptDetailSizeId());
                rd.setReturDetailPrice(receiptDetail.getReceiptDetailPrice());
                rd.setReturDetailDescription(receiptDetail.getReceiptDetailDescription());
                datas.add(rd);
            }
        } catch (Exception e) {
            Logger.getInstance().log(e);
        }
    }
}
